home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: tday.slip.netcom.com!user
- From: tday@netcom.com (Tony Day)
- Subject: Help with Bug Pleeeeease!
- Message-ID: <tday-2801961838030001@tday.slip.netcom.com>
- Sender: netnews@mork.netcom.com
- Nntp-Posting-Host: tday.slip.netcom.com
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- Date: Mon, 29 Jan 1996 01:38:03 GMT
-
-
- I hope some C++ wizard out there can help me debug this apparantly simple
- program. I am teaching myself C++ with Prata╣s │C++ primer +▓ and am
- completely stuck with one of the problems at the end of CH11.
-
- The aim of the excercise is to overload the +operator for a String Class
- (which consists of a char * and int holding the string length). I have
- written 3 overloaded + operators, one adds a String to a String (this
- seems to work), the other two add a C string to a String.
-
- The problem is with the latter two. When the assignment operator (not
- written by me) is invoked after one of these + operators, the whole system
- crashes (I am using Symantec Think C++ 7.04 on a Mac quadra) even if the
- assignment operator invokes different Strings to the + operators. This is
- what happens when the main (below) is run. If the assignment call is
- before the +operator call, no problem.
-
- I have put îcout╣s everywhere to try and track what is going on. The
- program crashes at │str = new char[len + 1]▓ in the = operator. The
- problem appears to be caused by │String temp = *this+tempo;▓ in the +
- operators.
-
- Another strange thing (that happens in all programs) is that temporary
- class variables which are created in a function and then returned by value
- are not destructed until the program ends. Is this what is meant to
- happen?
-
- Many thanks to whichever kind souls can solve this and give me my sanity back.
-
- Tony
-
- Source code follows:
-
-
-
- MAIN FILE
-
- //pe11_11.1.cpp
- #include <iostream.h>
- #include "oztrng2.h"
-
- int main(void)
- {
- String s1(" and I am a C++ student.");
- String s2 = "Please enter your name: ";
- String s3;
- cout << s2;
- cin >> s3;
-
- String temp = s3 + "my name is ";
- s1=s2;
-
- cout << "Bye\n";
- return 0;
- }
-
- HEADER FILES
-
- // booly.h -- Boolean definitions
- // eventually to be replaced by new C++ bool type
- #ifndef _BOOLY_H_
- #define _BOOLY_H_
- enum Bool {False, True}; // False = 0, True = 1
- #endif
-
-
- // strng2.h -- String class definition
- #ifndef _STRNG2_H_
- #define _STRNG2_H_
- #include <iostream.h>
- #include "booly.h" // our definitions of Bool, False, and True
- class String
- {
- private:
- char * str; // pointer to string
- int len; // length of string
- public:
- String(const char * s); // constructor
- String(); // default constructor
- String(const String & st);
- ~String(); // destructor
- int length () { return len; }
-
- // overloaded operators
- String & operator=(const String & st); // Assignment operator
- String & operator=(const char * s); // Assignment operator #2
- String operator+(const String & st) const;
- String operator+(const char * s) const;
-
- // friend functions
-
- friend String operator+(const char * s, const String & st);
- friend Bool operator>(const String &st1, const String &st2);
- friend Bool operator<(const String &st, const String &st2);
- friend Bool operator==(const String &st, const String &st2);
- friend ostream & operator<<(ostream & os, const String & st);
- friend istream & operator>>(istream & is, String & st);
- };
- #endif
-
- CLASS DEFINITION FILE
-
- // strng2.cpp -- String class methods
- #include <iostream.h>
- #include <string.h>
- #include <ctype.h>
- #include "oztrng2.h"
-
-
- // class methods
-
- String::String(const char * s) // make String from C string
- {
- len = strlen(s);
- str = new char[len + 1]; // allot storage
- strcpy(str, s); // initialize pointer
- cout << "created c++ String" <<str;
- }
-
- String::String() // default constructor
- {
- len = 0;
- str = new char[1];
- str[0] = '\0'; // default string
- cout << "create default String";
- }
-
- String::String(const String & st) // copy constructor
- {
- len = st.len;
- str = new char[len + 1];
- strcpy(str, st.str);
- cout << "create String string (copy)"<<str << "\n";
- }
-
- String::~String() // destructor
- {
- cout << "destroy "<< str<< "\n";
- delete [] str; // required
-
- }
-
- // assign a String to a String
- String & String::operator=(const String & st)
- { cout << "top of assign";
- if (this == &st)
- return *this;
- delete [] str;
- len = st.len;
- str = new char[len + 1];
- strcpy(str, st.str);
- cout << "assign STRING\n";
- return *this;
- }
-
- // assign a C string to a String
- String & String::operator=(const char * s)
- {
- delete [] str;
- len = strlen(s);
- str = new char[len + 1];
- strcpy(str, s);
- cout << "assign string\n";
- return *this;
- }
-
- String String::operator+(const String & st) const
- {
- String temp1;
- delete [] temp1.str;
- temp1.len = st.len+len;
- temp1.str = new char[temp1.len+1];
- strcpy(temp1.str,str);
- for (int i=len; i< temp1.len; i++)
- temp1.str[i]=st.str[i-len];
- temp1.str[temp1.len+1]='\0';
- cout << temp1.len << " length1st\n";
- return temp1;
- }
-
- String String::operator+(const char * s) const
- {
- String tempo;
- delete [] tempo.str;
- tempo.len=strlen(s);
- tempo.str = new char[tempo.len+1];
- strcpy(tempo.str,s);
- String temp = *this+tempo;
- cout << temp.len << " length2nd\n";
- return temp;
- }
-
- String operator+(const char * s, const String & st)
- {
- String tempo;
- delete [] tempo.str;
- tempo.len=strlen(s);
- tempo.str = new char[tempo.len+1];
- strcpy(tempo.str,s);
- String temp = tempo+st;
- cout << temp.len << " length3rd\n";
- return temp;
- }
-
- // true if st1 follows st2 in collating sequence
- Bool operator>(const String &st1, const String &st2)
- {
- if (strcmp(st1.str, st2.str) > 0)
- return True;
- else
- return False;
- }
-
-
- // true if st1 precedes st2 in collating sequence
- Bool operator<(const String &st1, const String &st2)
- {
- if (strcmp(st1.str, st2.str) < 0)
- return True;
- else
- return False;
- }
-
- // true if st1 is the same as st2
- Bool operator==(const String &st1, const String &st2)
- {
- if (strcmp(st1.str, st2.str) == 0)
- return True;
- else
- return False;
- }
-
- // display string
- ostream & operator<<(ostream & os, const String & st)
- {
- os << st.str;
- return os;
- }
-
- // quick and dirty String input
- istream & operator>>(istream & is, String & st)
- {
- char temp[80];
- is.getline(temp, 80);
- if (is)
- st = temp;
- return is;
- }
-